package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.constant.GameTypeConstant;
import com.ruoyi.system.outto.dto.HttpUtil;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.goods.NEWLEYOUAPI;
import com.ruoyi.system.outto.goods.NEWLXYAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/8/16 22:58
 */
@Slf4j
@Service
public class NEWLEYOUApiImpl implements NEWLEYOUAPI {

    @Value("${desc.title}")
    private String descTitle;

    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Autowired
    private CommonApi commonApi;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, sa);
        String requestp = JSONObject.toJSONString(params);
        log.info("乐游添加接口请求参数={}", requestp);
        String result = HttpUtils.sendPost(sa.getTwoOther()+"/open/v1/push-goods", JSONObject.toJSONString(params), "application/json");
        log.info("乐游添加接口返回参数={}", MapUntils.unicodeToString(result));
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"0".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
            String data = resultJson.getString("data");
            String goods_id = JSONObject.parseObject(data).getString("goods_id");
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(sa.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            if(CollectionUtils.isNotEmpty(artRecs)) {
                artRecs.get(0).setOutid(goods_id);
                artRecMapper.updateArtRec(artRecs.get(0));
            }else{
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                artRecreq.setType(LCConstant.interAdd);
                artRecreq.setConfig(sa.getId());
                artRecreq.setSaleCode(sa.getSaleName());
                artRecreq.setOutid(goods_id);
                artRecMapper.insertArtRec(artRecreq);
            }
        }
        resultMap.put("req", requestp);
        resultMap.put("res", MapUntils.unicodeToString(result));
        resultMap.put("msg", resultJson.toJSONString());

        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("uid",sa.getOther());
        params.put("timestamp",timestamp/1000);
        params.put("nonce","K4JyfYxDNt");
        params.put("app_key",sa.getSaleKey());
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1).replace("&",",");
        System.out.println("cover===="+cover);
        String sign = DigestUtils.sha256Hex(cover);
        params.put("sign",sign);
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(sa.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        params.put("goods_id",artRecs.get(0).getOutid());
        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal, goodsInfoAndAttrValueDto.getId(), sa);
        params.put("price",bigDecimal);
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        List<String> list = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(infoImgs)) {
            for(GoodsInfoImg goodsInfoImg1 : infoImgs){
                list.add(goodsInfoImg1.getImgUrl());
            }
        }else{
            list.add(LCConstant.defaultImg);
        }
        params.put("image_url", list);
        log.info("乐游编辑接口请求参数={}", params);
        String result = HttpUtils.sendPost(sa.getTwoOther()+"/open/v1/operate-goods", JSONObject.toJSONString(params), "application/json");
        log.info("乐游编辑接口返回参数={}", MapUntils.unicodeToString(result));
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"0".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
        }
        resultMap.put("req", params);
        resultMap.put("res", MapUntils.unicodeToString(result));
        resultMap.put("msg", resultJson.toJSONString());
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("uid",sa.getOther());
        params.put("timestamp",timestamp/1000);
        params.put("nonce","K4JyfYxDNt");
        params.put("app_key",sa.getSaleKey());
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1).replace("&",",");
        System.out.println("cover===="+cover);
        String sign = DigestUtils.sha256Hex(cover);
        params.put("sign",sign);
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(sa.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        params.put("goods_id",artRecs.get(0).getOutid());
        params.put("status","2");
//        params.put("is_delete","1");
        log.info("乐游删除接口请求参数={}", params);
        String result = HttpUtils.sendPost(sa.getTwoOther()+"/open/v1/operate-goods", JSONObject.toJSONString(params), "application/json");
        log.info("乐游删除接口返回参数={}", MapUntils.unicodeToString(result));
        JSONObject resultJson = JSONObject.parseObject(result);
        if (!"0".equals(resultJson.getString("code"))) {
            resultMap.put("code", 500);
        } else {
            resultMap.put("code", 200);
        }
        resultMap.put("req", params);
        resultMap.put("res", MapUntils.unicodeToString(result));
        resultMap.put("msg", resultJson.toJSONString());
        return AjaxResult.success(resultMap);
    }

    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        Map<String, Object> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("uid",sa.getOther());
        params.put("timestamp",timestamp/1000);
        params.put("nonce","K4JyfYxDNt");
        params.put("app_key",sa.getSaleKey());
        String cover = MapUntils.getCover(params);
        cover = cover.substring(0,cover.length()-1).replace("&",",");
        System.out.println("cover===="+cover);
        String sign = DigestUtils.sha256Hex(cover);
        params.put("sign",sign);
        params.remove("app_key");
        String attribute = "";
        if(goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
            params.put("game_id", 2);
            String reginal = goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName();
            if(reginal.equals("安卓QQ")){
                params.put("game_area_id",5);
                params.put("game_server_id",10);
            }else if(reginal.equals("安卓微信")){
                params.put("game_area_id",6);
                params.put("game_server_id",410);
            }else if(reginal.equals("苹果QQ")){
                params.put("game_area_id",7);
                params.put("game_server_id",672);
            }else if(reginal.equals("苹果微信")){
                params.put("game_area_id",8);
                params.put("game_server_id",792);
            }else{
                params.put("game_area_id",8);
                params.put("game_server_id",792);
            }
            String result = getAttribute("2");
            String data = JSONObject.parseObject(result).getString("data");
            JSONArray jsonArray1 = JSONArray.parseArray(data);
            String pfIds = "";
//            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF5());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            List<GameAttribute> gameAttributeList = getGameAttributs(pfIds);
            for(Object o : jsonArray1){
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("贵族等级")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(Object p : jsonArray11){
                            if(JSONObject.parseObject(p.toString()).getString("name").equals(gameAttribute1.getFieldName())){
                                attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }else{
                        attribute+=",6_67_无贵族";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("荣耀典藏")){
                    String string = "";
                    String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                    JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                    for(GameAttribute gameAttribute1 : gameAttributeList){
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("name"))){
                                string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",7_78_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("星元皮肤")){
                    String string = "";
                    String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                    JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                    for(GameAttribute gameAttribute1 : gameAttributeList){
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("name"))){
                                string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=string.substring(1);
                    }else{
                        attribute+=",8_92_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("稀有限定")){
                    String string = "";
                    String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                    JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                    for(GameAttribute gameAttribute1 : gameAttributeList){
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().contains(JSONObject.parseObject(p.toString()).getString("name"))){
                                string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",10_144_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("优质皮肤")){
                    String string = "";
                    String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                    JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                    for(GameAttribute gameAttribute1 : gameAttributeList){
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().contains(JSONObject.parseObject(p.toString()).getString("name"))){
                                attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",11_198_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("传说皮肤")){
                    String string = "";
                    String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                    JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                    for(GameAttribute gameAttribute1 : gameAttributeList){
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("name"))){
                                attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=string.substring(1);
                    }else{
                        attribute+=",9_103_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("限定角色")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF5())){
                        List<GameAttribute> gameAttributeLists = getGameAttributs(goodsInfoAndAttrValueDto.getF5());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeLists){
                            for(Object p : jsonArray11){
                                if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("name"))){
                                    attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }else{
                        attribute+=",12_229_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("王者段位")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(Object p : jsonArray11){
                            if(gameAttribute1.getFieldName().equals(JSONObject.parseObject(p.toString()).getString("name"))){
                                attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }else{
                        attribute+=",18_281_青铜";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("英雄皮肤数量")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())){
                        attribute+=","+JSONObject.parseObject(o.toString()).getString("id")+"_0_"+goodsInfoAndAttrValueDto.getF8();
                    }else{
                        attribute+=","+JSONObject.parseObject(o.toString()).getString("id")+"_0_0";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("英雄数量")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF7())){
                        attribute+=","+JSONObject.parseObject(o.toString()).getString("id")+"_0_"+goodsInfoAndAttrValueDto.getF7();
                    }else{
                        attribute+=","+JSONObject.parseObject(o.toString()).getString("id")+"_0_0";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("实名情况")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF16())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        if(!gameAttribute1.getFieldName().equals("已删实名")) {
                            for (Object p : jsonArray11) {
                                if (gameAttribute1.getFieldName().replace("包人脸", "支持人脸包赔").equals(JSONObject.parseObject(p.toString()).getString("name"))) {
                                    attribute += "," + JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }else{
                            attribute +=",54_2313_不可二次实名";
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("王者营地")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF17())){
                        attribute+=",55_0_"+goodsInfoAndAttrValueDto.getF17().replace(" ","");
                    }
                }
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().equals("和平精英")){
            params.put("game_id", 6);
            String reginal = goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName();
            if(reginal.equals("安卓QQ")){
                params.put("game_area_id",944);
                params.put("game_server_id",945);
            }else if(reginal.equals("安卓微信")){
                params.put("game_area_id",943);
                params.put("game_server_id",946);
            }else if(reginal.equals("苹果QQ")){
                params.put("game_area_id",942);
                params.put("game_server_id",947);
            }else if(reginal.equals("苹果微信")){
                params.put("game_area_id",941);
                params.put("game_server_id",948);
            }
            String result = getAttribute("6");
            String data = JSONObject.parseObject(result).getString("data");
            JSONArray jsonArray1 = JSONArray.parseArray(data);
            for(Object o : jsonArray1) {
                if (JSONObject.parseObject(o.toString()).getString("group_name").equals("实名情况")) {
                    if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF10())) {
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
                        if(gameAttribute1.getFieldName().equals("可二次实名")){
                            attribute+=",37_2239_可二次实名";
                        }else if(gameAttribute1.getFieldName().equals("不可二次实名")){
                            attribute+=",37_2240_不可二次实名";
                        }else if(gameAttribute1.getFieldName().equals("包人脸")){
                            attribute+=",37_2241_支持人脸包赔";
                        }else{
                            attribute+=",37_2240_不可二次实名";
                        }
                    }else{
                        attribute+=",37_2239_可二次实名";
                    }
                }
                if (JSONObject.parseObject(o.toString()).getString("group_name").equals("有无防沉迷")) {
                    attribute+=",38_2258_无防沉迷";
                }
                if (JSONObject.parseObject(o.toString()).getString("group_name").equals("王牌印记")) {
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF16())){
                        String attributse = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray = JSONArray.parseArray(attributse);
                        for(Object p : jsonArray){
                            if(JSONObject.parseObject(p.toString()).getString("name").replace("王牌印记","").equals(goodsInfoAndAttrValueDto.getF16())){
                                attribute+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                            }
                        }
                    }
                    attribute+=",39_2261_无";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("优质套装")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF5())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF5());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                if(JSONObject.parseObject(p.toString()).getString("name").contains(gameAttribute1.getFieldName())){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",40_2262_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("优质枪皮肤")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF1())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF1());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(gameAttribute1.getFieldName())){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",41_2263_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("稀有载具")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF2());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(gameAttribute1.getFieldName())){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",42_2264_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("稀有飞行器")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF3());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(gameAttribute1.getFieldName())){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",43_2265_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("优质降落伞")){
                    attribute+=",44_2266_无";
                }
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().equals("原神")){
            params.put("game_id", 1);
            if(goodsInfoAndAttrValueDto.getCarrierName().equals("游戏官方")) {
                params.put("game_area_id", 1);
                params.put("game_server_id", 3);
            }else{
                params.put("game_area_id", 2);
                params.put("game_server_id", 4);
            }
            String result = getAttribute("1");
            String data = JSONObject.parseObject(result).getString("data");
            JSONArray jsonArray1 = JSONArray.parseArray(data);
            for(Object o : jsonArray1) {
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("五星角色")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF11())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF11());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                String fieldName = gameAttribute1.getFieldName();
                                String a = fieldName.substring(0,2);
                                String b = fieldName.substring(2,fieldName.length());
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(b+" * "+a)){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",1_2335_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("五星武器")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF17())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF17());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                String fieldName = gameAttribute1.getFieldName();
                                String a = fieldName.substring(1,2);
                                String b = fieldName.substring(2,fieldName.length());
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(b+" * "+a+"精")){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",2_2343_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("账号来源")){
                    attribute+=",3_62_自己注册";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("主角信息")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
                        if(gameAttribute1.getFieldName().equals("男主")){
                            attribute+=",4_64_男主";
                        }else{
                            attribute+=",4_65_女主";
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("邮箱状态")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                        if(gameAttribute1.getFieldName().contains("已实名")){
                            attribute+=",30_801_邮箱已实名";
                        }else if(gameAttribute1.getFieldName().contains("未实名")){
                            attribute+=",30_802_邮箱未实名";
                        }else if(gameAttribute1.getFieldName().contains("未绑定邮箱")){
                            attribute+=",30_1400_邮箱未绑定";
                        }else if(gameAttribute1.getFieldName().contains("Q")){
                            attribute+=",30_1409_QQ邮箱";
                        }else{
                            attribute+=",30_1410_其他邮箱";
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("四星角色")){
                    attribute+=",29_2415_无";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("绑定情况")){
                    attribute+=",33_1756_手机号";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("游戏实名")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
                        if(gameAttribute1.getFieldName().contains("有修改人脸机会")){
                            attribute+=",34_1760_有实名信息";
                        }else{
                            attribute+=",34_1761_无实名信息";
                        }
                    }
                }
            }
        }
        if(goodsInfoAndAttrValueDto.getGameName().equals("崩坏·星穹铁道")){
            params.put("game_id", 8);
            params.put("game_area_id", 937);
            params.put("game_server_id", 938);
            String result = getAttribute("8");
            String data = JSONObject.parseObject(result).getString("data");
            JSONArray jsonArray1 = JSONArray.parseArray(data);
            for(Object o : jsonArray1) {
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("五星角色")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF6())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF6());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                String fieldName = gameAttribute1.getFieldName();
                                String [] strings = fieldName.split("魂");
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(strings[1]+" * "+strings[1].substring(0,1)+"命")){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",24_2296_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("五星光锥")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF7())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF7());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                String fieldName = gameAttribute1.getFieldName();
                                String [] strings = fieldName.split("影");
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(strings[1]+" * "+strings[1].substring(0,1)+"精")){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("账号来源")){
                    attribute+=",26_741_自己注册";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("四星角色")){
                    String string = "";
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())){
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF8());
                        String attributes = JSONObject.parseObject(o.toString()).getString("attribute");
                        JSONArray jsonArray11 = JSONArray.parseArray(attributes);
                        for(GameAttribute gameAttribute1 : gameAttributeList){
                            for(Object p : jsonArray11){
                                String fieldName = gameAttribute1.getFieldName();
                                String [] strings = fieldName.split("魂");
                                if(JSONObject.parseObject(p.toString()).getString("name").equals(strings[1]+" * "+strings[1].substring(0,1)+"命")){
                                    string+=","+JSONObject.parseObject(p.toString()).getString("option_id");
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotEmpty(string)){
                        attribute+=","+string.substring(1);
                    }else{
                        attribute+=",31_2310_无";
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("绑定情况")){
                    attribute+=",35_1764_手机号";
                }
                if(JSONObject.parseObject(o.toString()).getString("group_name").equals("游戏实名")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF4())){
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF4()));
                        if(gameAttribute1.getFieldName().contains("是")){
                            attribute+=",36_1766_有实名信息";
                        }else{
                            attribute+=",36_1767_无实名信息";
                        }
                    }
                }
            }
        }
        params.put("external_goods_sn",goodsInfoAndAttrValueDto.getId());

        params.put("title","【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId() + "】"+goodsInfoAndAttrValueDto.getDes().replace("+",""));
        params.put("level",20);
        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal, goodsInfoAndAttrValueDto.getId(), sa);
        params.put("price",bigDecimal);
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        List<String> list = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(infoImgs)) {
            for(GoodsInfoImg goodsInfoImg1 : infoImgs){
                list.add(goodsInfoImg1.getImgUrl());
            }
        }else{
            list.add(LCConstant.defaultImg);
        }
        params.put("image_url", list);
        params.put("description","【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId() + "】"+goodsInfoAndAttrValueDto.getDes().replace("+",""));
        params.put("attribute", attribute.substring(1));
        return params;
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        List<GameAttribute> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(ids)) {
            list = gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                    Long.class));
        }
        return list;
    }
    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    private String regionalCover(String game, String regional) {
        if (game.equals(GameTypeConstant.WZRY)) {
            if (Objects.equals("安卓QQ", regional)) {
                return "4";
            } else if (Objects.equals("安卓微信", regional)) {
                return "5";
            } else if (Objects.equals("苹果微信", regional)) {
                return "7";
            } else if (Objects.equals("苹果QQ", regional)) {
                return "6";
            } else {
                return null;
            }
        } else if (game.equals(GameTypeConstant.HPJY)) {
            if (Objects.equals("安卓QQ", regional)) {
                return "11";
            } else if (Objects.equals("安卓微信", regional)) {
                return "13";
            } else if (Objects.equals("苹果微信", regional)) {
                return "12";
            } else if (Objects.equals("苹果QQ", regional)) {
                return "10";
            } else {
                return null;
            }
        } else if (game.equals(GameTypeConstant.YS)) {
            return "4";
        } else if(game.equals(GameTypeConstant.WZGF)){
            if (Objects.equals("安卓QQ", regional)) {
                return "16";
            } else if (Objects.equals("安卓微信", regional)) {
                return "17";
            } else if (Objects.equals("苹果微信", regional)) {
                return "19";
            } else if (Objects.equals("苹果QQ", regional)) {
                return "18";
            } else {
                return "20";
            }
        }
        return null;
    }

    public static void main(String[] args) {
        Map<String, String> params = new TreeMap<>();
        Map<String, Object> header = new TreeMap<>();
        params.put("game_id","8");
//        String result = HttpUtil.doGet("https://cph.leyoo888.com/open/v1/get-servers", params);
        String result = getAttribute("8");
        System.out.println(MapUntils.unicodeToString(result));
    }

    static String getGame(){
        Map<String, String> params = new TreeMap<>();
        Map<String, Object> header = new TreeMap<>();
        String result = HttpUtil.doGet("https://cph.leyoo888.com/open/v1/get-games", params);
        return result;
    }

    static String getAttribute(String gameId){
        Map<String, String> params = new TreeMap<>();
        Map<String, Object> header = new TreeMap<>();
        params.put("game_id",gameId);
        String result = HttpUtil.doGet("https://cph.leyoo888.com/open/v1/get-game-attr-group", params);
        return result;
    }
}
